use std::collections::HashMap;

struct Solution {}

fn main() {}

impl Solution {
    pub fn can_construct(ransom_note: String, magazine: String) -> bool {
        let note_cnt = Self::get_char_cnt(ransom_note);
        let magazine_cnt = Self::get_char_cnt(magazine);
        Self::check(note_cnt, magazine_cnt)
    }

    pub fn get_char_cnt(note: String) -> HashMap<String, i32> {
        let mut char_cnt: HashMap<String, i32> = HashMap::new();
        for cha in note.as_str().chars() {
            let x = cha.to_string().as_str().to_owned();
            if char_cnt.contains_key(&x) {
                let i = char_cnt.get(&x).unwrap() + 1;
                char_cnt.insert(x, i);
            } else {
                char_cnt.insert(x, 1);
            }
        }
        char_cnt
    }

    pub fn check(ransom_cnt: HashMap<String, i32>, magazine_cnt: HashMap<String, i32>) -> bool {
        for entry in ransom_cnt.iter() {
            if !magazine_cnt.contains_key(entry.0) {
                return false;
            }
            if magazine_cnt.get(entry.0).unwrap() < entry.1 {
                return false;
            }
        }
        true
    }
}